package com.engine4cloud.tdf.supervision.convert;

import com.engine4cloud.tdf.common.core.constant.SupervisionBusinessConstants;
import com.engine4cloud.tdf.common.core.util.lambda.Actions;
import com.engine4cloud.tdf.supervision.dto.create.RcglVerificationEvidenceCreateDTO;
import com.engine4cloud.tdf.supervision.dto.result.RcglVerificationEvidenceResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.RcglVerificationEvidenceUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.RcglVerificationEvidenceEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Mapper
public interface RcglVerificationEvidenceConvert {

    RcglVerificationEvidenceConvert INSTANCE = Mappers.getMapper(RcglVerificationEvidenceConvert.class);

    RcglVerificationEvidenceEntity createTo(RcglVerificationEvidenceCreateDTO s);

    RcglVerificationEvidenceEntity updateTo(RcglVerificationEvidenceUpdateDTO s);

    RcglVerificationEvidenceResultDTO convertTo(RcglVerificationEvidenceEntity s);

    default RcglVerificationEvidenceEntity create(RcglVerificationEvidenceCreateDTO s, Long tenantId) {
        RcglVerificationEvidenceEntity entity = createTo(s);
        entity.setTenantId(tenantId);
        additionalArea(entity, s.getAreaList());
        return entity;
    }

    default RcglVerificationEvidenceEntity update(RcglVerificationEvidenceUpdateDTO s) {
        RcglVerificationEvidenceEntity entity = updateTo(s);
        additionalArea(entity, s.getAreaList());
        return entity;
    }

    default RcglVerificationEvidenceResultDTO convert(RcglVerificationEvidenceEntity s) {
        RcglVerificationEvidenceResultDTO result = convertTo(s);
        renderAreaList(result);
        return result;
    }

    default void additionalArea(RcglVerificationEvidenceEntity s, List<String> areaList) {
        if (Objects.isNull(s) || CollectionUtils.isEmpty(areaList)) {
            return;
        }
        s.setHsqzszs(areaList.get(SupervisionBusinessConstants.AREA_PROVINCE_INDEX));
        s.setHsqzszds(areaList.get(SupervisionBusinessConstants.AREA_CITY_INDEX));

        if (areaList.size() >= SupervisionBusinessConstants.AREA_COUNTY_ARRAY_LENGTH) {
            s.setHsqzszxq(areaList.get(SupervisionBusinessConstants.AREA_COUNTY_INDEX));
        } else {
            s.setHsqzszxq(null);
        }

        if (areaList.size() >= SupervisionBusinessConstants.AREA_VILLAGE_ARRAY_LENGTH) {
            s.setHsqzszd(areaList.get(SupervisionBusinessConstants.AREA_VILLAGE_INDEX));
        } else {
            s.setHsqzszd(null);
        }
    }

    default void renderAreaList(RcglVerificationEvidenceResultDTO s) {
        if (Objects.isNull(s)) {
            return;
        }

        List<String> residenceAreaList = new ArrayList<>();
        Actions.apply(StringUtils.isNotEmpty(s.getHsqzszs()), () -> residenceAreaList.add(s.getHsqzszs()));
        Actions.apply(StringUtils.isNotEmpty(s.getHsqzszds()), () -> residenceAreaList.add(s.getHsqzszds()));
        Actions.apply(StringUtils.isNotEmpty(s.getHsqzszxq()), () -> residenceAreaList.add(s.getHsqzszxq()));
        Actions.apply(StringUtils.isNotEmpty(s.getHsqzszd()), () -> residenceAreaList.add(s.getHsqzszd()));

        s.setAreaList(residenceAreaList);
    }
}
